﻿/*
 * marqdown
 * --------
 * a C# Markdown processor
 * 
 * Copyright (c) 2010 Andrew Wall
 * http://code.google.com/p/marqdown/
 * 
 * 
 */


#region Copyright and license

/*

Copyright (c) 2010 Andrew Wall

 http://www.opensource.org/licenses/mit-license.php
  
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#endregion


using System;
namespace Marqdown
{
    namespace StateMachine
    {
        public class Header1 : CompetitiveState
        {
            internal static ConcreteState header1EOL = new Header1EOL();
            internal static ConcreteState header2 = new Header2EOL();
            internal static ConcreteState headerNL = new HeaderNewLine();
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case '#':
                        info.ChangeState(Header1.header2);
                        break;
                    case '\n':
                        // Newline too early, so not actually a header
                        info.competitorFailed();
                        break;
                    default:
                        info.ChangeState(Header1.header1EOL);
                        info.process(c);
                        break;
                }
            }
            public override void GetAction(ActionSetting a)
            {
                a.leading = "#";
                a.action = Info.nop;
            }
        }
        public class UnderlineHeader1 : CompetitiveState
        {
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case '=':
                        // Absorb
                        break;
                    case '\n':
                        info.tag.SetTagName("h1");
                        info.ChangeState(Header1.headerNL);
                        break;
                    default:
                        // Any other char, so not actually a header
                        info.competitorFailed();
                        break;
                }
            }
            public override void GetAction(ActionSetting a)
            {
                a.leading = "=";
                a.action = Info.nop;
            }
        }
        public class UnderlineHeader2 : CompetitiveState
        {
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case '-':
                        // Absorb
                        break;
                    case '\n':
                        if (info.tag.size() > 0)
                        {
                            info.tag.SetTagName("h2");
                            info.ChangeState(Header1.headerNL);
                        }
                        else
                        {
                            info.competitorFailed();
                        }
                        break;
                    default:
                        // Any other char, so not actually a header
                        info.competitorFailed();
                        break;
                }
            }
            public override void GetAction(ActionSetting a)
            {
                a.leading = "-";
                a.action = Info.nop;
            }
        }
        public class HeaderToEOL : CommonState
        {
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case '\n':
                        info.tag.NewLine();
                        info.ChangeState(Header1.headerNL);

                        break;
                    default:
                        info.tag.Append(c);
                        break;
                }
            }
        }
        public class Header1EOL : HeaderToEOL
        {
            public override void onEnter(Info info)
            {
                NewTag(info, "h1");
            }

        }
        public class Header2EOL : HeaderToEOL
        {
            public override void onEnter(Info info)
            {
                NewTag(info, "h2");
            }
        }
        public class HeaderNewLine : ConcreteState
        {
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case '\n':
                        break;
                    default:
                        info.tag.SetAppendage("\n\n");
                        info.NewTag();
                        ///This is the new Success for a CompetitiveState
                        info.ResendAsLeading(c);
                        break;
                }
            }
        }

    }
}

